/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Data;

namespace DNNContentBuilder 
{
	[Serializable]
	public class DataConnectionInfo 
	{
        
		// Entity member variables for database table epDataConnection
        
		private Int32 mDataConnectionId;
		private string mTableName;
		private Int32 mConnectionStringId;
		private string mPrimaryKeyName;
        
		// Calculated Expression member variables for database table epDataConnection
        
        
		// Entity properties for database table epDataConnection
        
		public Int32 DataConnectionId 
		{
			get { return mDataConnectionId; }
			set { mDataConnectionId = value; }
		}
        
        
		public string TableName 
		{
			get { return mTableName; }
			set { mTableName = value; }
		}
        
        
		public Int32 ConnectionStringId 
		{
			get { return mConnectionStringId; }
			set { mConnectionStringId = value; }
		}
        
        
		public string PrimaryKeyName 
		{
			get { return mPrimaryKeyName; }
			set { mPrimaryKeyName = value; }
		}
        
        
	}
    
	public class DataConnectionController : BaseController
	{
		public DataConnectionController() : base() {}
		public DataConnectionController(ConnectionProvider cp) : base(cp) {}

		public void Add(DataConnectionInfo DataConnectionInfo) 
		{
			base.GetDataProvider().DataConnectionAdd(DataConnectionInfo.TableName, DataConnectionInfo.ConnectionStringId, DataConnectionInfo.PrimaryKeyName);
		}
        
		public void Update(DataConnectionInfo DataConnectionInfo) 
		{
			base.GetDataProvider().DataConnectionUpdate(DataConnectionInfo.DataConnectionId, DataConnectionInfo.TableName, DataConnectionInfo.ConnectionStringId, DataConnectionInfo.PrimaryKeyName);
		}
        
		public void Delete(Int32 dataConnectionId) 
		{
			base.GetDataProvider().DataConnectionDelete(dataConnectionId);
		}

		public DataConnectionInfo GetItemByTableName(string tableName) 
		{
			using (IDataReader idr = base.GetDataProvider().DataConnectionGetByTableName(tableName))
			{
				DataConnectionInfo infoObject = (DataConnectionInfo)DotNetNuke.Common.Utilities.CBO.FillObject(idr, typeof(DataConnectionInfo));
				return infoObject;
			} 
		}
        
		public DataConnectionInfo GetItem(Int32 dataConnectionId) 
		{
			using (IDataReader idr = base.GetDataProvider().DataConnectionGet(dataConnectionId))
			{
				DataConnectionInfo infoObject = (DataConnectionInfo)DotNetNuke.Common.Utilities.CBO.FillObject(idr, typeof(DataConnectionInfo));
				return infoObject;
			} 
		}
        
		// Select query methods for database table DataConnection
        
		public ArrayList GetAllDataConnections() 
		{
			using (IDataReader idr = base.GetDataProvider().DataConnectionGetAllDataConnections())
			{
				ArrayList infoList = DotNetNuke.Common.Utilities.CBO.FillCollection(idr, typeof(DataConnectionInfo));
				return infoList;
			} 
		}
        
		// Relation methods for database table epDataConnection
        
		public ArrayList GetByConnectionString(Int32 connectionStringId) 
		{
			using (IDataReader idr = base.GetDataProvider().DataConnectionByConnectionString(connectionStringId))
			{
				ArrayList infoList = DotNetNuke.Common.Utilities.CBO.FillCollection(idr, typeof(DataConnectionInfo));
				return infoList;
			} 
		}
	}
}
